package com.vtuzx.kingdee.xqc_core.dao;

import com.mongodb.client.result.UpdateResult;
import com.vtuzx.core.bean.dto.VtuzxPage;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.db.mongo.impl.VtuzxDaoImpl;
import com.vtuzx.kingdee.xqc_core.constant.BillStatus;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Component
public class PurSettlementDao extends VtuzxDaoImpl {
    /** 集合名称 */
    public static final String NAME = "t_pur_settlement";

    public PurSettlementDao() {
        super(NAME);
    }

    /**
     * 新建单据
     *
     * @param _userId 用户ID
     * @param userName 用户名
     * @param doc 数据
     * @return 单据ID
     */
    public ObjectId insert(ObjectId _userId, String userName, Document doc) {
        doc.append("_createUserId", _userId);
        doc.append("createUserName", userName);
        doc.append("_updateUserId", _userId);
        doc.append("updateUserName", userName);
        return insertOne(doc);
    }

    /**
     * 新建单据
     *
     * @param _userId 用户ID
     * @param userName 用户名
     * @param doc 数据
     * @return 单据ID
     */
    public UpdateResult update(ObjectId _userId, String userName, String id, Document doc) {
        doc.append("_updateUserId", _userId);
        doc.append("updateUserName", userName);
        return updateById(id, doc);
    }

    /**
     * 审批单据
     *
     * @param _userId 用户ID
     * @param userName 用户名
     * @param id 数据编号
     */
    public void audit(ObjectId _userId, String userName, String id) {
        updateById(id,
                new Document("status", BillStatus.Audit.toString())
                        .append("auditTime", new Date())
                        .append("_auditUserId", _userId)
                        .append("auditUserName", userName));
    }

    /**
     * 反审批单据
     *
     * @param id 数据编号
     */
    public void unaudit(String id) {
        updateByIdRedirect(id,
                new Document("$set", new Document("status", BillStatus.Save.toString()))
                .append("$unset", new Document("auditTime", 1).append("_auditUserId", 1).append("auditUserName", 1))
        );
    }
}
